package com.cauli.manage.operationLog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cauli.manage.operationLog.mapper.OperationLogMapper;
import com.cauli.manage.operationLog.model.vo.OperationLogVO;
import com.cauli.manage.operationLog.model.dto.OperationLogQueryDTO;
import com.cauli.manage.operationLog.model.entity.OperationLog;
import com.cauli.manage.operationLog.service.OperationLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Cauli
 * @date 2023-3-23 15:16:02
 * @description 操作日志 服务类
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Override
    public Page getOperationLogPage(OperationLogQueryDTO operationLogQueryDTO) {
        return baseMapper.selectPage(operationLogQueryDTO.toPage(), operationLogQueryDTO.toLambdaQueryWrapper());
    }

    @Override
    public OperationLogVO getOperationLogById(Long operationLogId) {
        OperationLog operationLog = baseMapper.selectById(operationLogId);
        OperationLogVO operationLogVO = new OperationLogVO();
        if (operationLog != null) {
            BeanUtils.copyProperties(operationLog, operationLogVO);
        }
        return operationLogVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOperationLog(List<Long> operationLogIds) {
        for (Long operationLogId : operationLogIds) {
            // 根据操作日志ID获取操作日志信息
            OperationLog operationLog = baseMapper.selectById(operationLogId);
            baseMapper.deleteById(operationLog);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addOperationLog(OperationLog operationlog) {
        baseMapper.insert(operationlog);
    }
}
